<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>回测报告详情 - 低位龙头股分析系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        .card-hover:hover {
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
            transition: box-shadow 0.3s ease-in-out;
        }

        .positive-return {
            color: #28a745;
        }

        .negative-return {
            color: #dc3545;
        }

        .performance-chart {
            max-width: 100%;
            height: auto;
        }
    </style>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">低位龙头股分析系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/backtesting">回测系统</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/backtesting/settings">回测设置</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/news">市场新闻</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row mb-4">
            <div class="col">
                <h2><i class="bi bi-file-earmark-bar-graph"></i> 回测报告详情</h2>
                <p class="lead">{{ report_name }}</p>
            </div>
            <div class="col-auto">
                <a href="/backtesting" class="btn btn-outline-primary">
                    <i class="bi bi-arrow-left"></i> 返回回测列表
                </a>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header bg-light">
                        <h5 class="mb-0">回测摘要</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-bordered">
                                    <tbody>
                                        <tr>
                                            <th style="width: 200px;">生成时间</th>
                                            <td>{{ report.timestamp }}</td>
                                        </tr>
                                        <tr>
                                            <th>测试数量</th>
                                            <td>{{ report.summary.total_tests }}</td>
                                        </tr>
                                        <tr>
                                            <th>日期范围</th>
                                            <td>{{ report.summary.date_range }}</td>
                                        </tr>
                                        <tr>
                                            <th>平均收益率</th>
                                            <td
                                                class="{% if report.summary.overall_avg_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                                {{ "%.2f"|format(report.summary.overall_avg_return) }}%
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>超额收益</th>
                                            <td
                                                class="{% if report.summary.overall_excess_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                                {{ "%.2f"|format(report.summary.overall_excess_return) }}%
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>胜率</th>
                                            <td>{{ "%.2f"|format(report.summary.overall_win_rate) }}%</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div class="col-md-6">
                                {% if has_image %}
                                <div class="text-center">
                                    <img src="/backtesting/performance/{{ image_name }}" alt="性能图表"
                                        class="performance-chart img-fluid">
                                </div>
                                {% else %}
                                <div class="alert alert-info">
                                    <i class="bi bi-info-circle"></i> 未找到性能图表
                                </div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header bg-light">
                        <h5 class="mb-0">各时间段性能指标</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>时间段</th>
                                        <th>平均收益率</th>
                                        <th>最大收益率</th>
                                        <th>最小收益率</th>
                                        <th>标准差</th>
                                        <th>基准收益率</th>
                                        <th>超额收益</th>
                                        <th>胜率</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for period, metrics in report.performance_metrics.periods.items() %}
                                    <tr>
                                        <td>{{ period }}个月</td>
                                        <td
                                            class="{% if metrics.avg_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                            {{ "%.2f"|format(metrics.avg_return) }}%
                                        </td>
                                        <td
                                            class="{% if metrics.max_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                            {{ "%.2f"|format(metrics.max_return) }}%
                                        </td>
                                        <td
                                            class="{% if metrics.min_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                            {{ "%.2f"|format(metrics.min_return) }}%
                                        </td>
                                        <td>{{ "%.2f"|format(metrics.std_return) }}%</td>
                                        <td
                                            class="{% if metrics.avg_benchmark_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                            {{ "%.2f"|format(metrics.avg_benchmark_return) }}%
                                        </td>
                                        <td
                                            class="{% if metrics.avg_excess_return > 0 %}positive-return{% else %}negative-return{% endif %}">
                                            {{ "%.2f"|format(metrics.avg_excess_return) }}%
                                        </td>
                                        <td>{{ "%.2f"|format(metrics.avg_win_rate) }}%</td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header bg-light d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">回测结果详情</h5>
                        <div>
                            <button class="btn btn-sm btn-outline-primary" id="toggle-details">
                                <i class="bi bi-chevron-down"></i> 展开详情
                            </button>
                        </div>
                    </div>
                    <div class="card-body" id="details-container" style="display: none;">
                        <div class="accordion" id="resultsAccordion">
                            {% for i in range(report.performance_metrics.total_tests) %}
                            {% set date = report.performance_metrics.dates[i] %}
                            <div class="accordion-item">
                                <h2 class="accordion-header" id="heading{{ i }}">
                                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                                        data-bs-target="#collapse{{ i }}">
                                        {{ date }}
                                    </button>
                                </h2>
                                <div id="collapse{{ i }}" class="accordion-collapse collapse"
                                    data-bs-parent="#resultsAccordion">
                                    <div class="accordion-body">
                                        <p>该日期的回测结果详情将在此显示。</p>
                                        <p>由于回测结果数据量较大，此处仅显示摘要信息。完整数据请查看原始报告文件。</p>
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header bg-light">
                        <h5 class="mb-0">回测结论</h5>
                    </div>
                    <div class="card-body">
                        <div class="alert alert-primary">
                            <h6><i class="bi bi-lightbulb"></i> 策略表现分析</h6>
                            <p>
                                {% if report.summary.overall_avg_return > 10 %}
                                该策略在回测期间表现优异，平均收益率达到 {{ "%.2f"|format(report.summary.overall_avg_return) }}%，
                                超额收益 {{ "%.2f"|format(report.summary.overall_excess_return) }}%，胜率 {{
                                "%.2f"|format(report.summary.overall_win_rate) }}%。
                                建议在实盘中采用该策略。
                                {% elif report.summary.overall_avg_return > 0 %}
                                该策略在回测期间表现良好，平均收益率为 {{ "%.2f"|format(report.summary.overall_avg_return) }}%，
                                超额收益 {{ "%.2f"|format(report.summary.overall_excess_return) }}%，胜率 {{
                                "%.2f"|format(report.summary.overall_win_rate) }}%。
                                建议在实盘中谨慎采用该策略，并密切关注市场变化。
                                {% else %}
                                该策略在回测期间表现不佳，平均收益率为 {{ "%.2f"|format(report.summary.overall_avg_return) }}%，
                                超额收益 {{ "%.2f"|format(report.summary.overall_excess_return) }}%，胜率 {{
                                "%.2f"|format(report.summary.overall_win_rate) }}%。
                                建议调整策略参数后再进行回测。
                                {% endif %}
                            </p>
                        </div>

                        <h6>改进建议：</h6>
                        <ul>
                            <li>考虑调整策略参数，优化选股标准</li>
                            <li>增加更多技术指标，提高选股精度</li>
                            <li>尝试不同的持有周期，找到最优持有时间</li>
                            <li>结合行业轮动因素，优化行业配置</li>
                            <li>考虑市场环境因素，在不同市场环境下采用不同策略</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function () {
            // 切换详情显示
            $('#toggle-details').click(function () {
                const detailsContainer = $('#details-container');
                const icon = $(this).find('i');

                if (detailsContainer.is(':visible')) {
                    detailsContainer.hide();
                    icon.removeClass('bi-chevron-up').addClass('bi-chevron-down');
                    $(this).html('<i class="bi bi-chevron-down"></i> 展开详情');
                } else {
                    detailsContainer.show();
                    icon.removeClass('bi-chevron-down').addClass('bi-chevron-up');
                    $(this).html('<i class="bi bi-chevron-up"></i> 收起详情');
                }
            });
        });
    </script>
</body>

</html>